package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.InventoryBatchBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/11/21
 * @Describe
 */
public class InventoryBatchAdapter extends RecyclerView.Adapter<InventoryBatchAdapter.BatchViewHolder> {
    private List<InventoryBatchBean> inventoryBatchList;
    private Context context;
    private OnEditTextChangeListener changeListener;

    public InventoryBatchAdapter(Context context) {
        this.context = context;
        this.inventoryBatchList = new ArrayList<>();
    }

    /**
     * 分页
     *
     * @param list
     * @param pageNo
     */
    public void setList(List<InventoryBatchBean> list, int pageNo) {
        if (pageNo == 1) {
            inventoryBatchList.clear();
        }
        inventoryBatchList.addAll(list);
        notifyDataSetChanged();
    }

    public List<InventoryBatchBean> getList() {
        return inventoryBatchList;
    }

    public void clear(){
        inventoryBatchList.clear();
        notifyDataSetChanged();
    }

    @Override
    public BatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BatchViewHolder(LayoutInflater.from(context).inflate(R.layout.item_batch, parent, false));
    }

    @Override
    public void onBindViewHolder(final BatchViewHolder holder, final int position) {
        InventoryBatchBean batchBean = inventoryBatchList.get(position);
        SimpleDateFormat simpleDateFormat0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = simpleDateFormat0.parse(batchBean.getExpiry_time());
            Date date_birth = simpleDateFormat0.parse(batchBean.getCreation_time());

            holder.tv_end_time.setText(simpleDateFormat1.format(date));
            holder.tv_birth_date.setText(simpleDateFormat1.format(date_birth));
            holder.tv_kucun_num.setText(batchBean.getInventory_keep()+"");
//            if (batchBean.getChange_num() == 0) {
//                holder.et_goods_num.setText(batchBean.getInventory_keep() + "");
//            }else {
                holder.et_goods_num.setText(batchBean.getChange_num() + "");
//            }
            holder.num = Long.valueOf(holder.et_goods_num.getText().toString());
            holder.iv_decrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != changeListener)changeListener.onDecrease(position);
                }
            });
            holder.iv_increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != changeListener)changeListener.onIncrease(position);
                }
            });
            holder.et_goods_num.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != changeListener)changeListener.onClick(position);
                }
            });
            holder.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != changeListener){
                        changeListener.onDelete(position);
                    }
                }
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return inventoryBatchList.size();
    }

    public void deleteItem(int position) {
        inventoryBatchList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 将新添加过来的批次信息展示到列表上，
     * 不进行网络请求，不然用户以前盘过的数量会重置
     * @param batchBean
     */
    public void setNewBatch(InventoryBatchBean batchBean) {
        boolean isContains = false;
        for (int i = 0; i < inventoryBatchList.size(); i++) {
            if (inventoryBatchList.get(i).getExpiry_time().equals(batchBean.getExpiry_time())) {
                inventoryBatchList.get(i).setChange_num(batchBean.getChange_num());
                isContains = true;
                notifyItemChanged(i);
                break;
            }
        }
        if (!isContains) {
            inventoryBatchList.add(batchBean);
            notifyItemInserted(inventoryBatchList.size() -1 );
        }
    }

    /**
     * 改变商品列表里的数量
     * @param position
     * @param count
     */
    public void updateData(int position, long count) {
        inventoryBatchList.get(position).setChange_num(count);
        notifyItemChanged(position);
    }

    public class BatchViewHolder extends RecyclerView.ViewHolder {
        TextView tv_end_time, tv_birth_date, tv_kucun_num;
        ImageView iv_decrease, iv_increase, iv_del;
        TextView et_goods_num;
        long num;

        public BatchViewHolder(View itemView) {
            super(itemView);
            tv_end_time = (TextView) itemView.findViewById(R.id.tv_end_time);
            tv_birth_date = (TextView) itemView.findViewById(R.id.tv_birth_date);
            tv_kucun_num = (TextView) itemView.findViewById(R.id.tv_kucun_num);
            iv_decrease = (ImageView) itemView.findViewById(R.id.iv_decrease);
            iv_increase = (ImageView) itemView.findViewById(R.id.iv_increase);
            et_goods_num = (TextView) itemView.findViewById(R.id.et_goods_num);
            iv_del = (ImageView) itemView.findViewById(R.id.iv_del);
        }
    }

    public void setChangeListener(OnEditTextChangeListener changeListener) {
        this.changeListener = changeListener;//EditText改变
    }


    public interface OnEditTextChangeListener {
        void onClick(int position);
        void onDecrease(int position);
        void onIncrease(int position);
        void onDelete(int position);
    }
}
